package baidu;

import java.net.*;
import java.io.*;
import java.security.cert.*;
import javax.net.ssl.*;

/*   HTTP/HTTPS请求.同一个Http对象,cookie共享。 */

public class Http
{
	private String cookie = "";

	public void setCookie(String cookie)
	{
		this.cookie = cookie;
	}

	public String getCookie()
	{
		return cookie;
	}

	public String get(String url,String charset)
	{
		StringBuilder sb = new StringBuilder();
		try{
			URL _url = new URL(url);
		if(url.toLowerCase().contains("https://"))
		{
			HttpsHandler httpsH = new HttpsHandler();
			httpsH.trustAllHttpsCertificates();
			HostnameVerifier hv = new HostnameVerifier(){
				public boolean verify(String urlHostName, SSLSession session) {
					return true;
				}
			};
			HttpsURLConnection.setDefaultHostnameVerifier(hv);
			HttpsURLConnection sconn = (HttpsURLConnection)_url.openConnection();
			sconn.setRequestProperty("Cookie",cookie);
			BufferedReader br = new BufferedReader(new InputStreamReader(sconn.getInputStream(),charset));
			String temp = null;
			while((temp = br.readLine())!=null)
			{
				sb.append(temp);
				sb.append("\n");
			}
			br.close();
			String cookie = sconn.getHeaderField("Set-Cookie");
			if(cookie !=null){
				this.cookie = cookie;
			}
		}
		else
		{
			HttpURLConnection conn = (HttpURLConnection)_url.openConnection();
			conn.setRequestProperty("Cookie",cookie);
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),charset));
			String temp = null;
			while((temp = br.readLine())!=null)
			{
				sb.append(temp);
				sb.append("\n");
			}
			br.close();
			String cookie = conn.getHeaderField("Set-Cookie");
			if(cookie !=null){
				this.cookie = cookie;
			}
		}
		}catch(Exception e){return null;}
		return sb.toString();
	}

	public String post(String url,String data,String charset)
	{
		StringBuilder sb = new StringBuilder();
		try{
		URL _url = new URL(url);
		if(url.toLowerCase().contains("https://"))
		{
			HttpsHandler httpsH = new HttpsHandler();
			httpsH.trustAllHttpsCertificates();
			HostnameVerifier hv = new HostnameVerifier(){
				public boolean verify(String urlHostName, SSLSession session) {
					return true;
				}
			};
			HttpsURLConnection.setDefaultHostnameVerifier(hv);
			HttpsURLConnection sconn = (HttpsURLConnection)_url.openConnection();
			sconn.setDoInput(true);
			sconn.setDoOutput(true);
			sconn.setRequestProperty("Cookie",cookie);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sconn.getOutputStream(),charset));
			bw.write(data);
			bw.flush();
			BufferedReader br = new BufferedReader(new InputStreamReader(sconn.getInputStream(),charset));
			String temp = null;
			while((temp = br.readLine())!=null)
			{
				sb.append(temp);
				sb.append("\n");
			}
			br.close();
			String cookie = sconn.getHeaderField("Set-Cookie");
			if(cookie !=null){
				this.cookie = cookie;
			}
		}
		else
		{
			HttpURLConnection conn = (HttpURLConnection)_url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestProperty("Cookie",cookie);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(),charset));
			bw.write(data);
			bw.flush();
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),charset));
			String temp = null;
			while((temp = br.readLine())!=null)
			{
				sb.append(temp);
				sb.append("\n");
			}
			br.close();
			bw.close();
			String cookie = conn.getHeaderField("Set-Cookie");
			if(cookie !=null){
				this.cookie = cookie;
			}
		}
		}catch(Exception e){return null;}
		return sb.toString();
	}

	public byte[] file(String url)
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try{
		URL _url = new URL(url);
		HttpURLConnection conn = (HttpURLConnection)_url.openConnection();
		conn.setRequestProperty("Cookie",cookie);
		BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
		byte[] b = new byte[1024];
		int len = -1;
		while((len = bis.read(b))!=-1)
		{
			bos.write(b,0,len);
			bos.flush();
		}
		bos.close();
		bis.close();
		String cookie = conn.getHeaderField("Set-Cookie");
		if(cookie !=null){
			this.cookie = cookie;
		}
		}catch(Exception e){return null;}
		return bos.toByteArray();
	}






	private class HttpsHandler {
		public void trustAllHttpsCertificates() throws Exception {
			TrustManager[] tm_array = new TrustManager[1];
			TrustManager tm = new MyTrustManager();
			tm_array[0] = tm;
			SSLContext sc = SSLContext.getInstance("SSL");
			sc.init(null, tm_array, null);
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		}
		private class MyTrustManager implements TrustManager, X509TrustManager {
			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
			public boolean isServerTrusted(X509Certificate[] certs) {
				return true;
			}
			public boolean isClientTrusted(X509Certificate[] certs) {
				return true;
			}
			public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
				return;
			}
			public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
				return;
			}
		}
	}
}
